package com.jy.adscm.operation.mscyb.controller;


import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.mscyb.bean.JqcsMachineTestProecss;
import com.jy.adscm.operation.mscyb.service.JqcsMachineTestProecssService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyl
 * @since 2019-06-13
 */
@RestController
@RequestMapping("/adscm/operation/jqcs/jqcsMachineTestProecss")
public class JqcsMachineTestProecssController {
    @Autowired
    JqcsMachineTestProecssService jqbzMaterialListCheckService;
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;

    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody JqcsMachineTestProecss jqcsMachineTestProecss, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        jqcsMachineTestProecss.setCreator(token.getUser().getId().toString());
        jqcsMachineTestProecss.setCreatTime(new Date());
        boolean insert = jqbzMaterialListCheckService.insert(jqcsMachineTestProecss);
        return new SimpleCoreResponse("200","新增成功",insert);
    }
    @RequestMapping("update")
    public SimpleCoreResponse update(@RequestBody List<JqcsMachineTestProecss> jqcsMachineTestProecsss, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        for (JqcsMachineTestProecss jqcsMachineTestProecss : jqcsMachineTestProecsss) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(jqcsMachineTestProecss.getValue(),null)){
                jqcsMachineTestProecss.setCreator(token.getUser().getId().toString());
                jqcsMachineTestProecss.setCreatTime(new Date());
                boolean result = jqbzMaterialListCheckService.updateById(jqcsMachineTestProecss);
            }
        }
        return new SimpleCoreResponse("200","修改成功",true);
    }
    @RequestMapping("selectByPid/{pid}")
    public SimpleCoreResponse selectByPid(@PathVariable String pid, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        List<JqcsMachineTestProecss> jqbzMaterialListChecks=jqbzMaterialListCheckService.selectByPid(pid);
        return new SimpleCoreResponse("200","查询成功",jqbzMaterialListChecks);
    }
}

